package api

// 通用响应封装结构体
type Result struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data,omitempty"`
}

func NewResult(code int, msg string, data interface{}) *Result {
	return &Result{Code: code, Msg: msg, Data: data}
}

// 常量定义状态码和消息
const (
	SuccessCode = 200
	ErrorCode   = 500
)
const (
	SuccessMsg = "操作成功"
	ErrorMsg   = "操作失败"
)

// 成功响应
func OkWithData(data interface{}) *Result {
	return NewResult(SuccessCode, SuccessMsg, data)
}
func OkWithMsg(msg string) *Result {
	return NewResult(SuccessCode, msg, nil)
}
func Ok(msg string, data interface{}) *Result {
	return NewResult(SuccessCode, msg, data)
}

// 失败响应
func ErrWithData(data interface{}) *Result {
	return NewResult(ErrorCode, SuccessMsg, data)
}
func ErrWithMsg(msg string) *Result {
	return NewResult(ErrorCode, msg, nil)
}
func Err(msg string, data interface{}) *Result {
	return NewResult(ErrorCode, msg, data)
}
